package com.bjx.generator.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.generator.domain.entity.TableFieldEntity;
import com.bjx.generator.mapper.TableFieldMapper;
import com.bjx.generator.service.ITableFieldService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc: 表字段
 * @author: liushuai
 * @date: 2022/9/14 17:36
 */
@Service
public class TableFieldServiceImpl extends ServiceImpl<TableFieldMapper,TableFieldEntity> implements ITableFieldService {

    @Resource
    private TableFieldMapper tableFieldMapper;


    @Override
    public void saveTableFieldBatchByTableId(List<TableFieldEntity> entitys,Long tableId) {
        deleteTableFieldByTableId(tableId);
        if(CollectionUtil.isEmpty(entitys)){
            return;
        }
        entitys.forEach(res ->{
            res.setTableId(tableId);
            res.setFieldId(null);
        });
        this.saveBatch(entitys);

    }

    @Override
    public List<TableFieldEntity> listTableFieldByTableId(Long tableId) {
        LambdaQueryWrapper<TableFieldEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TableFieldEntity::getTableId,tableId);
        return tableFieldMapper.selectList(wrapper);
    }

    @Override
    public void deleteTableFieldByTableId(Long tableId) {
        LambdaQueryWrapper<TableFieldEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TableFieldEntity::getTableId,tableId);
        tableFieldMapper.delete(wrapper);
    }
}
